<?php
class Project_model extends CI_Model {

	public function __construct()
	{
		$this->load->database();
		$this->load->library('session');
	}
	
	public function get_news($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('news');
			return $query->result_array();
		}
		
		$query = $this->db->get_where('news', array('slug' => $slug));
		return $query->row_array();
	}
	
	public function set_news()
	{
		$this->load->helper('url');
		
		$slug = url_title($this->input->post('title'), 'dash', TRUE);
		
		$data = array(
			'title' => $this->input->post('title'),
			'slug' => $slug,
			'text' => $this->input->post('text')
		);
		
		return $this->db->insert('news', $data);
	}
	
	public function login()
	{
		$ret;
		$ret['type'] = -1;
		
		
		$query = $this->db->get('Zdravotnicky_personal');
		foreach ($query->result_array() as $row)
		{
			if ( ($row['Rodne_cislo'] == $this->input->post('username')) && ($row['Heslo'] == $this->input->post('password')) ) {
				if ($row['Specializacia'] == 'Sestra') {
					$ret['type'] = 1;
				}
				else {
					$ret['type'] = 2;
				}
				
				$ret['id'] = $this->input->post('username');
			}
		}
		
		$query = $this->db->get('Pacient');
		foreach ($query->result_array() as $row)
		{
			if ( ($row['Rodne_cislo'] == $this->input->post('username')) && ($row['Heslo'] == $this->input->post('password')) ) {
				$ret['type'] = 0;
				$ret['id'] = $this->input->post('username');
			}
		}
		

		$this->session->set_userdata($ret);
		
		return $ret['type'];
	}
	
	public function get_dept($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('Oddelenie');
			return $query->result_array();
		}
		
		$query = $this->db->get_where('Oddelenie', array('Nazov_oddelenia' => $slug));
		return $query->row_array();
		
		
	}
}